package com.mxpio.erp.plm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mxpio.erp.common.plm.bo.ProjectTaskDeliverableBO;
import com.mxpio.erp.common.plm.entity.*;
import com.mxpio.erp.common.plm.service.*;
import com.mxpio.erp.common.plm.vo.ProjectTaskDetailVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectTaskAuditRecordsServiceImpl extends BaseServiceImpl<ProjectTaskAuditRecords> implements ProjectTaskAuditRecordsService {

    @Autowired
    private ProjectTaskExecutionLogService projectTaskExecutionLogService;
    @Autowired
    private ProjectTaskDeliverableDraftService projectTaskDeliverableDraftService;
    @Autowired
    private ProjectTaskCompleteDeliverableDraftService projectTaskCompleteDeliverableDraftService;
    @Autowired
    private ProjectTaskEndDeliverableDraftService projectTaskEndDeliverableDraftService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> getDetail(ProjectTaskAuditRecords projectTaskAuditRecords) {
        ProjectTaskDetailVO projectTaskDetailVO = new ProjectTaskDetailVO();
        //变更
        if (projectTaskAuditRecords.getOperationalNodes().equals("10")){
            ProjectTaskExecutionLog byId = projectTaskExecutionLogService.getById(ProjectTaskExecutionLog.class,projectTaskAuditRecords.getTaskLogId());
            List<ProjectTaskDeliverableDraft> deliverableDrafts = projectTaskDeliverableDraftService.list(ProjectTaskDeliverableDraft.class, Criteria.create().addCriterion("taskLogId", Operator.EQ, projectTaskAuditRecords.getTaskLogId()));
            List<ProjectTaskDeliverableBO> projectTaskDeliverableBOS = new ArrayList<>();
            projectTaskDetailVO.setTaskExecutionLog(byId);
            for (ProjectTaskDeliverableDraft projectTaskDeliverableDraft : deliverableDrafts) {
                ProjectTaskDeliverableBO projectTaskDeliverableBO = new ProjectTaskDeliverableBO();
                BeanUtil.copyProperties(projectTaskDeliverableDraft,projectTaskDeliverableBO);
                projectTaskDeliverableBOS.add(projectTaskDeliverableBO);
            }
            projectTaskDetailVO.setProjectTaskDeliverableBOS(projectTaskDeliverableBOS);
            return Result.OK(projectTaskDetailVO);
        }
        //关闭
        if (projectTaskAuditRecords.getOperationalNodes().equals("20")){
            ProjectTaskExecutionLog byId = projectTaskExecutionLogService.getById(ProjectTaskExecutionLog.class,projectTaskAuditRecords.getTaskLogId());
            List<ProjectTaskEndDeliverableDraft> deliverableDrafts = projectTaskEndDeliverableDraftService.list(ProjectTaskEndDeliverableDraft.class, Criteria.create().addCriterion("taskLogId", Operator.EQ, projectTaskAuditRecords.getTaskLogId()));
            projectTaskDetailVO.setTaskExecutionLog(byId);
            List<ProjectTaskDeliverableBO> projectTaskDeliverableBOS = new ArrayList<>();
            for (ProjectTaskEndDeliverableDraft projectTaskDeliverableDraft : deliverableDrafts) {
                ProjectTaskDeliverableBO projectTaskDeliverableBO = new ProjectTaskDeliverableBO();
                BeanUtil.copyProperties(projectTaskDeliverableDraft,projectTaskDeliverableBO);
                projectTaskDeliverableBOS.add(projectTaskDeliverableBO);
            }
            projectTaskDetailVO.setProjectTaskDeliverableBOS(projectTaskDeliverableBOS);
            return Result.OK(projectTaskDetailVO);
        }
        //完工
        if (projectTaskAuditRecords.getOperationalNodes().equals("30")){
            ProjectTaskExecutionLog byId = projectTaskExecutionLogService.getById(ProjectTaskExecutionLog.class,projectTaskAuditRecords.getTaskLogId());
            List<ProjectTaskCompleteDeliverableDraft> deliverableDrafts = projectTaskCompleteDeliverableDraftService.list(ProjectTaskCompleteDeliverableDraft.class, Criteria.create().addCriterion("taskLogId", Operator.EQ, projectTaskAuditRecords.getTaskLogId()));
            projectTaskDetailVO.setTaskExecutionLog(byId);
            List<ProjectTaskDeliverableBO> projectTaskDeliverableBOS = new ArrayList<>();
            for (ProjectTaskCompleteDeliverableDraft projectTaskDeliverableDraft : deliverableDrafts) {
                ProjectTaskDeliverableBO projectTaskDeliverableBO = new ProjectTaskDeliverableBO();
                BeanUtil.copyProperties(projectTaskDeliverableDraft,projectTaskDeliverableBO);
                projectTaskDeliverableBOS.add(projectTaskDeliverableBO);
            }
            projectTaskDetailVO.setProjectTaskDeliverableBOS(projectTaskDeliverableBOS);
            return Result.OK(projectTaskDetailVO);
        }
        return null;
    }
}
